package com.ruoyi.web.controller.order;

import java.util.List;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.order.domain.SeasonProduct;
import com.ruoyi.order.domain.Vo.ProductVo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.order.domain.NewProduct;
import com.ruoyi.order.service.INewProductService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 新产品表Controller
 * 
 * @author ruoyi
 * @date 2022-02-04
 */
@Controller
@RequestMapping("/order/newProduct")
public class NewProductController extends BaseController
{
    private String prefix = "order/newProduct";

    @Autowired
    private INewProductService newProductService;

    @RequiresPermissions("order:newProduct:view")
    @GetMapping()
    public String newProduct()
    {
        return prefix + "/newProduct";
    }

    /**
     * 查询新产品表列表
     */
    @RequiresPermissions("order:newProduct:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(NewProduct newProduct)
    {
        startPage();
        List<NewProduct> list = newProductService.selectNewProductList(newProduct);
        return getDataTable(list);
    }

    /**
     * 导出新产品表列表
     */
    @RequiresPermissions("order:newProduct:export")
    @Log(title = "新产品表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(NewProduct newProduct)
    {
        List<NewProduct> list = newProductService.selectNewProductList(newProduct);
        ExcelUtil<NewProduct> util = new ExcelUtil<NewProduct>(NewProduct.class);
        return util.exportExcel(list, "新产品表数据");
    }

    /**
     * 新增新产品表
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存新产品表
     */
    @RequiresPermissions("order:newProduct:add")
    @Log(title = "新产品表", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(NewProduct newProduct)
    {
        newProduct.setCreateUser(getUserId().intValue());

        return toAjax(newProductService.insertNewProduct(newProduct));
    }

    /**
     * 修改新产品表
     */
    @RequiresPermissions("order:newProduct:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Integer id, ModelMap mmap)
    {
        NewProduct newProduct = newProductService.selectNewProductById(id);
        mmap.put("newProduct", newProduct);
        return prefix + "/edit";
    }

    /**
     * 修改保存新产品表
     */
    @RequiresPermissions("order:newProduct:edit")
    @Log(title = "新产品表", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(NewProduct newProduct)
    {
        newProduct.setUpdateUser(getUserId().intValue());
        return toAjax(newProductService.updateNewProduct(newProduct));
    }

    /**
     * 删除新产品表
     */
    @RequiresPermissions("order:newProduct:remove")
    @Log(title = "新产品表", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)

    {
        return toAjax(newProductService.deleteNewProductByIds(ids));
    }




    /**
     * 模糊查询热销产品列表信息
     */
    @PostMapping("/getNewProductPage")
    @ResponseBody
    public TableDataInfo getSeasonProductPage(String productName )
    {
        startPage();
        List<ProductVo> list = newProductService.getNewProductPage(productName);
        return getDataTable(list);
    }

    /**
     * 查询热销产品列表信息
     */
    @PostMapping("/getNewProductPageList")
    @ResponseBody
    public TableDataInfo getNewProductPageList()
    {
        startPage();
        List<ProductVo> list = newProductService.getNewProductPageList();
        return getDataTable(list);
    }

    /**
     * 查询单个热销产品信息
     */
    @PostMapping("/addNewProduct")
    @ResponseBody
    public AjaxResult addNewProduct(@RequestParam("productIDs") Integer[]  productIDs)
    {
        int count = 0;
        if (productIDs !=null && productIDs.length > 0 ){
            for (int i = 0; i < productIDs.length; i++) {
                NewProduct newProduct = new NewProduct() ;
                newProduct.setProductID(productIDs[i]);
                newProduct.setCreateTime(DateUtils.getNowDate());
                count +=newProductService.insertNewProduct(newProduct);
            }
        }
        return toAjax(count);
    }
}
